package org.zasadnyy.redlions.players.client.ui;

import java.util.Date;
import java.util.List;

import org.zasadnyy.redlions.players.shared.model.PlayerItem;
import org.zasadnyy.redlions.players.shared.remote.PlayersCrudRemote;
import org.zasadnyy.redlions.players.shared.remote.PlayersCrudRemoteAsync;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardPagingPolicy.KeyboardPagingPolicy;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.smartgwt.client.util.SC;

public class EditPlayersComposite extends Composite {

	interface MyUiBinder extends
			UiBinder<HorizontalPanel, EditPlayersComposite> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	private PlayersCrudRemoteAsync servise = GWT
			.create(PlayersCrudRemote.class);

	@UiField(provided = true)
	CellList<PlayerItem> cellList;

	@UiField(provided = true)
	PlayerEditPanel editPanel;

	static interface Images extends ClientBundle {
		// @Source("logo.gif")
		ImageResource contact();
	}

	static class PlayerCell extends AbstractCell<PlayerItem> {

		/**
		 * The html of the image used for contacts.
		 */
		// private final String imageHtml;
		//
		// public PlayerCell(ImageResource image) {
		// this.imageHtml = AbstractImagePrototype.create(image).getHTML();
		// }

		@Override
		public void render(Context context, PlayerItem value, SafeHtmlBuilder sb) {
			// Value can be null, so do a null check..
			if (value == null) {
				return;
			}

			sb.appendHtmlConstant("<table>");

			// Add the contact image.
			sb.appendHtmlConstant("<tr><td rowspan='3'>");
			// sb.appendHtmlConstant(imageHtml);
			sb.appendHtmlConstant("</td>");

			// Add the name and address.
			sb.appendHtmlConstant("<td style='font-size:95%;'>");
			sb.appendEscaped(value.getName() + " " + value.getSurname());
			sb.appendHtmlConstant("</td></tr><tr><td>");

			Date playsFrom = value.getPlaysFrom();
			if (playsFrom != null) {
				sb.appendEscaped(DateTimeFormat.getFormat(
						PredefinedFormat.DATE_LONG).format(playsFrom));
			}
			sb.appendHtmlConstant("</td></tr></table>");
		}

	}

	public EditPlayersComposite() {

		// Images imageBundle = GWT.create(Images.class);
		// ImageResource contact = imageBundle.contact();

		cellList = new CellList<PlayerItem>(new PlayerCell());
		editPanel = new PlayerEditPanel();

		initWidget(uiBinder.createAndBindUi(this));

		loadPlayers();

		cellList.setKeyboardPagingPolicy(KeyboardPagingPolicy.INCREASE_RANGE);
		cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.BOUND_TO_SELECTION);

		// Add a selection model so we can select cells.
		final SingleSelectionModel<PlayerItem> selectionModel = new SingleSelectionModel<PlayerItem>();
		cellList.setSelectionModel(selectionModel);
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						editPanel.setPlayer(selectionModel.getSelectedObject());
					}
				});

		editPanel.addValueChangeHandler(new ValueChangeHandler<PlayerItem>() {

			@Override
			public void onValueChange(ValueChangeEvent<PlayerItem> event) {
				loadPlayers();
				PlayerItem selectedObject = selectionModel.getSelectedObject();
				selectionModel.setSelected(selectedObject, true);
			}
		});

	}

	private void loadPlayers() {
		servise.getAllPlayers(new AsyncCallback<List<PlayerItem>>() {

			@Override
			public void onSuccess(List<PlayerItem> result) {
				cellList.setRowData(result);
				cellList.redraw();
			}

			@Override
			public void onFailure(Throwable caught) {
				SC.say("Things are bad!");
			}
		});
	}

}
